package Controller.TimerTasks;

import com.intouchtv.ari.NabDemoView;

import model.Movie;
import model.MovieEvent;
import model.EventProperties.ClickEventProperties;
import model.EventProperties.MoveablePictureEventProperties;
import Controller.GameController;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.util.Log;


public class StartEventTimerTask extends MovieTimerTask {

	public StartEventTimerTask(MovieEvent event, NabDemoView view,
			Movie currentMovie, GameController controller) {
		super(event, view, currentMovie, controller);
	}

	@Override
	public void run() {
		switch (event.getType()) {

		case click: {
			ClickEventProperties clickProp = (ClickEventProperties) event
					.getProperties();
			switch (clickProp.getElementProperties().getElementType()) {
			case button:
				view.AddButton(currentMovie.getId(), event.getId(), clickProp
						.getElementProperties().getLocation(), clickProp
						.getElementProperties().getColor(), clickProp
						.isVisible(), clickProp.getElementProperties()
						.getText(), clickProp.isShowClickEffect(), clickProp
						.getClickEffectColor());
				break;
			case pictureBox:
				try {
					
					BitmapFactory.Options options=new BitmapFactory.Options();
					options.inSampleSize = 1;
										
					Bitmap image = BitmapFactory.decodeFile(
							controller.mediaUrlToByteArray.get(clickProp.getUrl().getUrl()), options);
					
					currentMovie.frames.add(image);

					view.AddPictureBox(currentMovie.getId(), event.getId(),
							clickProp.getElementProperties().getLocation(),
							image, clickProp.getElementProperties().isFlickering(),
							event.resultYes != null && event.resultYes.length > 0);

				} catch (Exception e) {
					view.RaiseErrorMsg("error getting picture");
					return;
				}
				break;

			case text:
				view.AddTextBox(currentMovie.getId(), event.getId(), clickProp
						.getElementProperties().getLocation(), clickProp
						.getElementProperties().getColor(), clickProp
						.getElementProperties().getText(), clickProp
						.getElementProperties().isFlickering());
				break;

			default:
				break;
			}

			break;
		}// end of case click

		case tuochScreen:
			ClickEventProperties touchProp = (ClickEventProperties) event
					.getProperties();

			view.AddTextBox(currentMovie.getId(), event.getId(), touchProp
					.getElementProperties().getLocation(), Color.TRANSPARENT,
					 null,false);
			
			/*view.AddButton(currentMovie.getId(), event.getId(), touchProp
					.getElementProperties().getLocation(), Color.TRANSPARENT,
					false, null, touchProp.isShowClickEffect(), touchProp
							.getClickEffectColor());*/
			break;

		case motion:
			break;

		case showMovablePicture:
			MoveablePictureEventProperties picProp = (MoveablePictureEventProperties) event
					.getProperties();

			Long startTime = System.currentTimeMillis();
			Log.e("StartTimeByte", String.valueOf(startTime));

			Bitmap[] frames = new Bitmap[picProp.numOfFrames];
			for (int i = 0; i < frames.length; i++) {
				frames[i] = BitmapFactory.decodeFile(picProp
						.getFrameUrl(i).getUrl());
			}

			// check if this is an animated picture
			if (picProp.numOfFrames > 1) {
				view.AddMovableAnimatedGif(currentMovie.getId(), event.getId(),
						picProp.getPrimeryLocation(), picProp
								.getSeconderyLocation(), frames);
			} else {
				view.AddMovablePictureBox(currentMovie.getId(), event.getId(),
						picProp.getPrimeryLocation(), picProp
								.getSeconderyLocation(), frames[0]);
			}

			Long endTime = System.currentTimeMillis();
			Log.e("EndTime", String.valueOf(endTime));
			long totalTime = (endTime - startTime) / 1000;
			Log.e("TotalTime", String.valueOf(totalTime));

			break;

		case voice:
			break;

		case keybord:
			break;
		case readFlag:
		{
			//in this case there is no need to add anything to the view etc.
			//so this part is empty.
			break;
		}
		default:
			break;

		}
		controller.getEventManager().addListedEvent(event);
	}

}
